package com.kaesar.leetcode.LeetCode_001_050;

import java.util.*;

public class LeetCode_047 {

    /**
     * 构造一棵多叉树
     */
    static class MultiTree {
        // 当前的值
        public Integer val;

        public MultiTree parent;

        // 当前路径已经走过的数组的位置
        public List<Integer> used;

        // 当前路径中的数字
        public List<Integer> paths;

        public MultiTree(Integer val) {
            this.val = val;
            used = new ArrayList<>();
            paths = new ArrayList<>();
        }
    }

    public static List<List<Integer>> permuteUnique(int[] nums) {
        Queue<MultiTree> queue = new LinkedList<>();
        Arrays.sort(nums);
        int curNum = nums[0];
        // 第一条路径
        MultiTree first = new MultiTree(nums[0]);
        first.paths.add(nums[0]);
        first.used.add(0);
        queue.add(first);
        // 其他路径
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != curNum) {
                MultiTree next = new MultiTree(nums[i]);
                next.paths.add(nums[i]);
                next.used.add(i);
                queue.add(next);
                curNum = nums[i];
            }
        }

        int length = 1;

        while (length < nums.length) {
            int count = queue.size();
            while (count > 0) {
                MultiTree curNode = queue.poll();
                int firstNum = -1, firstNumIndex = -1;
                // 找到第一个已有路径没经过的数
                for (int i = 0; i < nums.length; i++) {
                    if (!curNode.used.contains(i)) {
                        firstNum = nums[i];
                        firstNumIndex = i;
                        MultiTree firstTree = new MultiTree(nums[i]);
                        firstTree.paths.addAll(curNode.paths);
                        firstTree.paths.add(firstNum);
                        firstTree.used.addAll(curNode.used);
                        firstTree.used.add(firstNumIndex);
                        queue.add(firstTree);
                        break;
                    }
                }

                // 将其他不同的数也添加到新的路径
                for (int i = firstNumIndex + 1; i < nums.length; i++) {
                    if (!curNode.used.contains(i) && nums[i] != firstNum) {
                        MultiTree otherTree = new MultiTree(nums[i]);
                        otherTree.paths.addAll(curNode.paths);
                        otherTree.paths.add(nums[i]);
                        otherTree.used.addAll(curNode.used);
                        otherTree.used.add(i);
                        queue.add(otherTree);
                        firstNum = nums[i];
                    }
                }
                count--;
            }
            length++;
        }

        List<List<Integer>> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            result.add(queue.poll().paths);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1, 2};
        for (List<Integer> integers : permuteUnique(nums)) {
            for (Integer integer : integers) {
                System.out.print(integer + " ");
            }
            System.out.println();
        }
    }
}
